package com.plian.system.controller.pf;

import com.plian.Tools.Func;
import com.plian.Tools.StringPool;
import com.plian.Tools.base.BaseController;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.Behavioral;
import com.plian.system.annotation.LogOperation;
import com.plian.system.config.Resubmit;
import com.plian.system.entity.pf.BaseOfficeScore;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.pf.IBaseOfficeScoreService;
import com.plian.system.service.wf.FormWorkflowService;
import com.plian.system.vo.pf.BaseOfficeScoreVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: Take-off
 * @Description: TODO
 * @Date: 2023/1/29 6:37 PM
 * @Version: 1.0
 **/
@RestController
@AllArgsConstructor
@RequestMapping("api/officescore")
@Api(value = "BaseOfficeScoreController", tags = "履职成效-责任处室评分 控制器")
public class BaseOfficeScoreController extends BaseController {

    private final IBaseOfficeScoreService baseOfficeScoreService;

    private FormWorkflowService formWorkflowService;

    @PostMapping("/detail")
    @Behavioral
    @ApiOperation(value = "详情", notes = "传入 baseOfficeScore")
    public R<BaseOfficeScoreVO> detail(@RequestBody BaseOfficeScore baseOfficeScore) {
        return baseOfficeScoreService.getDetailObj(baseOfficeScore);
    }

    @PostMapping("/list")
    @ApiOperation(value = "分页", notes = "传入query")
    public ListResult<BaseOfficeScoreVO> list(@RequestBody Query query) {
        ListResult<BaseOfficeScoreVO> listResult = baseOfficeScoreService.list(query);
        return listResult;
    }

    @PostMapping("/submit")
    @ApiOperation(value = "新增或修改", notes = "baseOfficeScore")
    @Resubmit(delaySeconds = 0)
    @LogOperation("履职成效-责任处室评分-提交")
    public R<BaseOfficeScore> submit(@Valid @RequestBody BaseOfficeScore baseOfficeScore) {
        boolean result = baseOfficeScoreService.submit(baseOfficeScore);

        R r = result ? R.data(baseOfficeScore) : new R(CommonCode.FAIL);
        HashMap<String, Object> processVariable = new HashMap<>();
        formWorkflowService.startProcessAndSimulate(String.valueOf(baseOfficeScore.getId()),baseOfficeScore.getFormType(),processVariable);

        return r;
    }

    @PostMapping("/remove")
    @ApiOperation(value = "删除", notes = "传入 ids")
    @LogOperation("履职成效-责任处室评分-删除")
    public R remove(@RequestBody PageData pd) {
        List<String> ids = (List<String>) pd.get("ids");
        return baseOfficeScoreService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA)))
                ? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
    }

//    @PostMapping("/sendMessage") TODO DEL
//    @ApiOperation(value = "发送消息", notes = "传入 id")
//    public R sendMessage(@RequestBody BaseOfficeScore baseOfficeScore) {
//         baseOfficeScoreService.sendMessage(baseOfficeScore);
//         return R.success();
//    }

    @PostMapping("/makeAllOffice")
    @ApiOperation(value = "生成责任处室", notes = "传入 id")
    @LogOperation("履职成效-责任处室评分-生成责任处室")
    public R makeAllOffice(@RequestBody BaseOfficeScore baseOfficeScore) {
         baseOfficeScoreService.makeAllOffice(baseOfficeScore.getFillingYear());
         return R.success();
    }

}
